package com.mym.system;

import java.util.Objects;

/**
 * @author miao
 * @version 1.0
 * @date 2025/4/15 19:02
 * @description
 */
public class ShangPing {
        //商品编号
        private int goodsId;
        //商品名称
        private GoodsName goodsName;
        //商品价格
        private double goodsPrice;
        //商品库存
        private int goodsStock;

    public ShangPing() {
    }

    public ShangPing(int goodsId, GoodsName goodsName, double goodsPrice, int goodsStock) {
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.goodsPrice = goodsPrice;
        this.goodsStock = goodsStock;
    }

    public int getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(int goodsId) {
        this.goodsId = goodsId;
    }

    public GoodsName getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(GoodsName goodsName) {
        this.goodsName = goodsName;
    }

    public double getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(double goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    public int getGoodsStock() {
        return goodsStock;
    }

    public void setGoodsStock(int goodsStock) {
        this.goodsStock = goodsStock;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ShangPing shangPing = (ShangPing) o;
        return goodsId == shangPing.goodsId && Double.compare(shangPing.goodsPrice, goodsPrice) == 0 && goodsStock == shangPing.goodsStock && goodsName == shangPing.goodsName;
    }

    @Override
    public int hashCode() {
        return Objects.hash(goodsId, goodsName, goodsPrice, goodsStock);
    }

    @Override
    public String toString() {
        return "ShangPing{" +
                "goodsId=" + goodsId +
                ", goodsName=" + goodsName +
                ", goodsPrice=" + goodsPrice +
                ", goodsStock=" + goodsStock +
                '}';
    }
   //初始化商品信息
   public void initGoodsInfo() {
       //创建数组存入商品信息
       ShangPing[] shangPings = new ShangPing[10];
       //商品编号随机生成在10000-11000之间的数字
       System.out.println("商品编号\t商品名称\t商品价格\t商品库存");

       shangPings[0] = new ShangPing((int) (Math.random() * 10000 + 10000), GoodsName.海尔冰箱, 3999.9, 50);
       shangPings[1] = new ShangPing((int) (Math.random() * 10000 + 10000), GoodsName.格力冰箱, 2888.8, 30);
       shangPings[2] = new ShangPing((int) (Math.random() * 10000 + 10000), GoodsName.TCL冰箱, 1999.9, 100);
       shangPings[3] = new ShangPing((int) (Math.random() * 10000 + 10000), GoodsName.美的冰箱, 4999.9, 60);
       shangPings[4] = new ShangPing((int) (Math.random() * 10000 + 10000), GoodsName.海尔冰箱, 3099.9, 50);
       shangPings[5] = new ShangPing((int) (Math.random() * 10000 + 10000), GoodsName.格力冰箱, 2808.8, 40);
       shangPings[6] = new ShangPing((int) (Math.random() * 10000 + 10000), GoodsName.TCL冰箱, 1099.8, 120);
       shangPings[7] = new ShangPing((int) (Math.random() * 10000 + 10000), GoodsName.美的冰箱, 4909.9, 65);
       for (int i = 0; i < shangPings.length; i++) {
           //商品编号为没有的不输出,输出有商品编号的商品信息
           if (shangPings[i] == null) {
               continue;

           }
           System.out.println(shangPings[i].goodsId + "\t" + shangPings[i].goodsName + "\t" + shangPings[i].goodsPrice + "\t" + shangPings[i].goodsStock);

       }
       System.out.println("请输入商品编号");
       int goodsId = User.scanner.nextInt();
       //确认商品编号是否存在
       loop:
       while (true) {
           //请输入商品编号

           for (int i = 0; i < shangPings.length; i++) {
               if (goodsId == shangPings[i].goodsId) {
                   System.out.println("商品编号：" + shangPings[i].goodsId + "\t商品名称：" + shangPings[i].goodsName + "\t商品价格：" + shangPings[i].goodsPrice + "\t商品库存：" + shangPings[i].goodsStock);
                   break loop;
               } else {
                   System.out.println("商品编号不存在");
               }
           }
       }
       koop:
       while (true) {
           //请输入购买数量
           System.out.println("请输入购买数量");
           int goodsStock = User.scanner.nextInt();
           //购买数量不能为负数和零库存以及购买数量小于50
           if (goodsStock <= 0 || goodsStock > 50) {
               System.out.println("购买数量不能为负数和大于库存以及小于50");
               continue koop;
           }
           System.out.println("购买成功");
           for (int i = 0; i < shangPings.length; i++) {
               if (goodsId == shangPings[i].goodsId) {
                   if (goodsStock <= shangPings[i].goodsStock) {
                       shangPings[i].goodsStock -= goodsStock;
                       break koop;

                   }
               }
           }

       }



//您还要继续购买吗
        while(true){
                System.out.println("您还要继续购买吗？（是Y 否任意键）");
                String s=User.scanner.next();
                if(s.equals("Y")){
                initGoodsInfo();
                }else{
                break;
                }

                }
                System.out.println("商品编号\t商品名称\t商品价格\t商品库存");
                for(int i=0;i<shangPings.length;i++){
        if(shangPings[i]==null){
        continue;
        }
        System.out.println(shangPings[i].goodsId+"\t"+shangPings[i].goodsName+"\t"+shangPings[i].goodsPrice+"\t"+shangPings[i].goodsStock);
        }


        }


        }

//创建商品名称类枚举
enum GoodsName {
    海尔冰箱,
    格力冰箱,
    TCL冰箱,
    美的冰箱;
}

